<?php

namespace PhpTool;

/**
 * Created by PhpStorm.
 * User: huxiaolong
 * Date: 2019/10/18
 * Time: 13:44
 */
class Reflection
{
    public static function getDocComment($class = '', $method = '', $type = '')
    {
        if (!class_exists($class) || empty($type)) return '';
        
        $ref = new \ReflectionClass($class);
        
        if (empty($method)) {
            return self::fetchDocComment($ref->getDocComment(), $type);
        }
        else {
            $methods = $ref->getMethods();
            foreach ($methods as $method) {
                if ($method->getName() == 'actionHomeworkStat') {
                    return self::fetchDocComment($method->getDocComment(), $type);
                }
            }
        }
        
        return '';
    }
    
    public static function getDocComments()
    {
        return;
    }
    
    //******************************************************************************
    
    private static function fetchDocComment($docComments = '', $type = '')
    {
        if (empty($docComments) || empty($type)) return '';
        
        $typesArr       = Arr::map(explode('|', $type), 'trim');
        $docCommentsArr = Arr::map(explode(PHP_EOL, $docComments), 'trim');
        foreach ($typesArr as $type) {
            foreach ($docCommentsArr as $v) {
                if (($pos = mb_stripos($v, $type)) !== false) {
                    return trim(mb_substr($v, $pos + mb_strlen($type)));
                }
            }
        }
        
        return '';
    }
    
}